package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.NoticeInfMapper;
import com.stylefeng.guns.modular.netcar.model.NoticeInf;
import com.stylefeng.guns.modular.netcar.service.INoticeInfService;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 通知信息 Service 实现 
 * @author sasazaixian
 * @Date 2018-11-15 16:09
 */
@Service
public class NoticeInfServiceImpl extends ServiceImpl<NoticeInfMapper,NoticeInf> implements INoticeInfService {

    @Resource
    private NoticeInfMapper noticeInfMapper;

    /**
     * 删除通知信息
     * @param noticeInf
     * @return     
     */
    @Transactional
    public int deleteNoticeInf(NoticeInf noticeInf,String token)throws Exception{
        if(noticeInf==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.noticeInfMapper.deleteNoticeInf(noticeInf);
    }
    /**
     * 批量删除通知信息
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchNoticeInf(String[] ids,String token)throws Exception{
        return this.noticeInfMapper.deleteBatchNoticeInf(ids);
    }
    /**
     * 查询通知信息列表
     * @param noticeInf 通知信息对象
     * @return
     */
    public List<NoticeInf> selectListNoticeInf(NoticeInf noticeInf,String token)throws Exception{
        return  this.noticeInfMapper.selectListNoticeInf(noticeInf);
    }
    /**
	 * 分页查询通知信息列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListNoticeInf(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(noticeInfMapper.selectListNoticeInf(page,(NoticeInf)pager.getObj()));
        List<NoticeInf> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param noticeInf 通知信息对象
     * @return 
     */
    public  List<NoticeInf> selectListNoticeInfForUpdate(NoticeInf noticeInf)throws Exception{
        return  this.noticeInfMapper.selectListNoticeInfForUpdate(noticeInf);
    }
    /**
     * 通过主键id查询的通知信息列表
     * @param id 主键id
     * @return     
     */
    public  NoticeInf findNoticeInfById(String id,String token)throws Exception{
        return  this.noticeInfMapper.findNoticeInfById(id);
    }

    /**
     * 新增通知信息数据
     * @param noticeInf  通知信息对象
     * @return
     */
    @Transactional
    public int insertNoticeInf(NoticeInf noticeInf,String token) throws Exception { 
        if(noticeInf!=null && (noticeInf.getNiId()==null || noticeInf.getNiId().trim().length()<10)){
            noticeInf.setNiId(IdWorker.get32UUID());
        }
        if(noticeInf.getNiLevel()==null){
            noticeInf.setNiLevel(10);
        }else if(!noticeInf.getNiLevel().equals("10")){
            if(noticeInf.getNiLevelStartDate() ==null || noticeInf.getNiLevelEndDate()==null || noticeInf.getNiLevelStartDate().compareTo(noticeInf.getNiLevelEndDate())<0){
                throw new RuntimeException("请输入正确的优先级开始和截止日期（yyyy-MM-dd）");
            }
        }
        if(noticeInf.getNiState()==null){
            noticeInf.setNiState("0");
        }
        return this.noticeInfMapper.insertNoticeInf(noticeInf);
    }
    
    /**
     * 更新通知信息数据
     * @param noticeInf 通知信息对象
     * @return     
     */
    @Transactional
    public int updateNoticeInf(NoticeInf noticeInf,String token) throws Exception {
        if(!noticeInf.getNiLevel().equals("10")){
            if(noticeInf.getNiLevelStartDate() ==null || noticeInf.getNiLevelEndDate()==null || noticeInf.getNiLevelStartDate().compareTo(noticeInf.getNiLevelEndDate())<0){
                throw new RuntimeException("请输入正确的优先级开始和截止日期（yyyy-MM-dd）");
            }
        }
        return  this.noticeInfMapper.updateNoticeInf(noticeInf);
    }    
    /**
     * 查询通知信息返回key-Value属性
     * @param noticeInf 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsNoticeInf(NoticeInf noticeInf,String token) throws Exception {
        List<NoticeInf> listNoticeInf=this.noticeInfMapper.selectListNoticeInf(noticeInf);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(NoticeInf item:listNoticeInf){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getNiId());
            opts.setValue(item.getNiId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
